package com.smart.community.property.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smart.community.commons.result.Result;
import com.smart.community.property.dto.ReminderRecordCreateDTO;
import com.smart.community.property.dto.ReminderRecordDTO;
import com.smart.community.property.dto.ReminderRecordEditDTO;
import com.smart.community.property.dto.ReminderRecordQueryDTO;
import com.smart.community.property.dto.SyncReminderToFeeDTO;
import com.smart.community.property.service.IReminderRecordService;
import com.smart.community.property.vo.ReminderRecordVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 嵌套催缴/沟通记录接口
 * 严格遵循《智慧社区微服务架构总览.md》《系统状态枚举权威标准声明.md》
 * @author Wu.Liang
 * @since 2025-07-22
 */
@Slf4j
@RestController
@RequestMapping("/property/fees/reminders")
@Tag(name = "嵌套催缴/沟通记录", description = "物业/卫生费嵌套催缴/沟通记录接口")
@Validated
public class ReminderRecordController {

    @Autowired
    private IReminderRecordService reminderRecordService;

    @PostMapping
    @Operation(summary = "新增嵌套记录")
    public Result<Long> add(@RequestBody @Valid ReminderRecordCreateDTO dto) throws Exception {
        ReminderRecordDTO recordDTO = convertToDTO(dto);
        Long id = reminderRecordService.addReminder(recordDTO);
        log.info("新增嵌套记录成功，ID: {}", id);
        return Result.success("新增成功", id);
    }

    @GetMapping
    @Operation(summary = "嵌套记录历史查询")
    public Result<List<ReminderRecordVO>> list(@Valid ReminderRecordQueryDTO queryDTO) throws Exception {
        List<ReminderRecordVO> list = reminderRecordService.listReminders(queryDTO.getFeeId(), queryDTO.getFeeType());
        log.info("查询嵌套记录成功，数量: {}", list.size());
        return Result.success(list);
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改嵌套记录")
    public Result<Boolean> update(@Parameter(description = "嵌套记录ID") @PathVariable Long id,
                                  @RequestBody @Valid ReminderRecordEditDTO dto) throws Exception {
        ReminderRecordDTO recordDTO = convertToDTO(dto);
        reminderRecordService.updateReminder(id, recordDTO);
        log.info("修改嵌套记录成功，ID: {}", id);
        return Result.success("修改成功", true);
    }

    @PostMapping("/sync-fee")
    @Operation(summary = "同步嵌套记录到主表")
    public Result<Boolean> syncToFee(@RequestBody @Valid SyncReminderToFeeDTO dto) throws Exception {
        ReminderRecordDTO recordDTO = convertToDTO(dto);
        reminderRecordService.syncFee(recordDTO);
        log.info("同步嵌套记录到主表成功");
        return Result.success("同步成功", true);
    }

    /**
     * DTO转换工具：CreateDTO/EditDTO/SyncDTO -> ReminderRecordDTO
     */
    private ReminderRecordDTO convertToDTO(ReminderRecordCreateDTO dto) {
        ReminderRecordDTO recordDTO = new ReminderRecordDTO();
        recordDTO.setFeeId(dto.getFeeId());
        recordDTO.setFeeType(dto.getFeeType());
        recordDTO.setReminderType(dto.getReminderType());
        recordDTO.setReminderContent(dto.getReminderContent());
        recordDTO.setNextDueDate(dto.getNextDueDate());
        recordDTO.setNextAmount(dto.getNextAmount());
        recordDTO.setRemark(dto.getRemark());
        return recordDTO;
    }
    private ReminderRecordDTO convertToDTO(ReminderRecordEditDTO dto) {
        ReminderRecordDTO recordDTO = new ReminderRecordDTO();
        recordDTO.setFeeId(dto.getFeeId());
        recordDTO.setFeeType(dto.getFeeType());
        recordDTO.setReminderType(dto.getReminderType());
        recordDTO.setReminderContent(dto.getReminderContent());
        recordDTO.setNextDueDate(dto.getNextDueDate());
        recordDTO.setNextAmount(dto.getNextAmount());
        recordDTO.setRemark(dto.getRemark());
        return recordDTO;
    }
    private ReminderRecordDTO convertToDTO(SyncReminderToFeeDTO dto) {
        ReminderRecordDTO recordDTO = new ReminderRecordDTO();
        recordDTO.setFeeId(dto.getFeeId());
        recordDTO.setFeeType(dto.getFeeType());
        recordDTO.setNextDueDate(dto.getNextDueDate());
        recordDTO.setNextAmount(dto.getNextAmount());
        return recordDTO;
    }
}